import {
  getBaseUrl,
  requestUtil
} from '../../utils/requestUtil.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    baseUrl: '',
    cart:[],
    allChecked:false,
    totalPrice:0,
    totalNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const baseUrl = getBaseUrl();
    this.setData({
      baseUrl
    })
  },

  handleChooseAddress(){
    wx.chooseAddress({
      success: (result) => {
        console.log(result)
        wx.setStorageSync('address', result)
      },
    })
  },
  //商品选中事件处理
  handleItemChange(e){
    const id = e.currentTarget.dataset.id
    let {cart} = this.data
    let index = cart.findIndex(v=>v.id === id)
    cart[index].checked = !cart[index].checked
    this.setCart(cart)
  },

  //处理结算事件
  handlePay(){
    const {address,totalNum} = this.data;
    if(!address){
      wx.showToast({
        title: '您还没有设置收货地址',
        icon:'none'
      })
      return
    }
    if(totalNum === 0){
      wx.showToast({
        title: '您还没有选购商品',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/pay/index'
    })

  },

  //重新计算底部工具栏：全选、总价、总数量、重新设置缓存
  setCart(cart){
    let allChecked = true
    //获取产品的总价格,获取产品的总数量
    let totalPrice = 0
    let totalNum = 0 
    cart.forEach(v=>{
      if(v.checked){
        totalPrice += v.price*v.num
        totalNum += v.num
      }else{
        allChecked = false
      }
    });
    allChecked = cart.length!=0?allChecked:false
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    })
    //重新设置缓存
    wx.setStorageSync('cart', cart)
  },
  //商品全选事件处理
  handleItemAllCheck(){
    let {cart,allChecked} = this.data
    allChecked = !allChecked
    cart.forEach(v=>v.checked = allChecked)
    this.setCart(cart)
  },

  //编辑商品数量事件
  handleItemNum(e){
    const {id,operation} = e.currentTarget.dataset
    let {cart} = this.data
    let index = cart.findIndex(v=>v.id === id)
    if(cart[index].num === 1 && operation === -1){
      //数量减少为0的时候提示用户
      wx.showModal({
        title: '系统提示',
        content: '您是否要删除该商品',
        success:(res)=>{
          if(res.confirm){
            cart.splice(index,1)
            this.setCart(cart)
          }
        }
      })
    }else{
      cart[index].num += operation
      this.setCart(cart)
    }
  },

  //减少商品数量事件
  handleItemNumDown(e){
    let {id} = e.currentTarget.dataset
    let {cart} = this.data
    cart.forEach(v=>{
      if(v.id===id){
        v.num ++;
      }
    })
    this.setData(cart)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const address = wx.getStorageSync('address')
    const cart = wx.getStorageSync('cart') || []
    this.setData({
      address
    })
    this.setCart(cart)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})